package com.shmilyou.dissertationmanagement.biz;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/2/26 */

import com.shmilyou.dissertationmanagement.domain.ThesisPublishType;
import com.shmilyou.dissertationmanagement.repository.ThesisPublishTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ThesisPublishTypeService {

    private final ThesisPublishTypeRepository thesisPublishTypeRepository;

    @Autowired
    public ThesisPublishTypeService(ThesisPublishTypeRepository thesisPublishTypeRepository) {
        this.thesisPublishTypeRepository = thesisPublishTypeRepository;
    }

    public ThesisPublishType queryById(Integer id) {
        Optional<ThesisPublishType> thesisPublishType = thesisPublishTypeRepository.findById(id);
        return thesisPublishType.orElse(null);
    }

    public ThesisPublishType save(ThesisPublishType entity) {
        return thesisPublishTypeRepository.save(entity);
    }

    public ThesisPublishType update(ThesisPublishType entity) {
        return thesisPublishTypeRepository.save(entity);
    }

    public ThesisPublishType delete(ThesisPublishType entity) {
        entity.setDel(1);
        return thesisPublishTypeRepository.save(entity);
    }

    public List<ThesisPublishType> queryAll() {
        return thesisPublishTypeRepository.findAllByDel(0);
    }

    public Page<ThesisPublishType> queryAll(int pageIndex, int pageSize) {
        Pageable page = PageRequest.of(pageIndex, pageSize);
        Page<ThesisPublishType> content = thesisPublishTypeRepository.findAllByDel(page, 0);
        return content;
    }
}
